[[...path]].page.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import React from 'react';
  2. import { IUserHasId, IPagePopulatedToShowRevision } from '@growi/core';
  3. import {
  4. GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import superjson from 'superjson';
  10. import { useCurrentGrowiLayoutFluidClassName } from '~/client/services/layout';
  11. import { MainPane } from '~/components/Layout/MainPane';
  12. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  13. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  14. import { Page } from '~/components/Page';
  15. import type { PageSideContentsProps } from '~/components/PageSideContents';
  16. import { DrawioViewerScript } from '~/components/Script/DrawioViewerScript';
  17. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  18. import { CrowiRequest } from '~/interfaces/crowi-request';
  19. import { RendererConfig } from '~/interfaces/services/renderer';
  20. import { IShareLinkHasId } from '~/interfaces/share-link';
  21. import type { PageDocument } from '~/server/models/page';
  22. import {
  23. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  24. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault, useDrawioUri, useIsContainerFluid,
  25. } from '~/stores/context';
  26. import loggerFactory from '~/utils/logger';
  27. import { NextPageWithLayout } from '../_app.page';
  28. import {
  29. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  30. } from '../utils/commons';
  31. const logger = loggerFactory('growi:next-page:share');
  32. const PageSideContents = dynamic<PageSideContentsProps>(() => import('~/components/PageSideContents').then(mod => mod.PageSideContents), { ssr: false });
  33. // const Comments = dynamic(() => import('~/components/Comments').then(mod => mod.Comments), { ssr: false });
  34. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  35. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  36. type Props = CommonProps & {
  37. shareLinkRelatedPage?: IShareLinkRelatedPage,
  38. shareLink?: IShareLinkHasId,
  39. isExpired: boolean,
  40. disableLinkSharing: boolean,
  41. isSearchServiceConfigured: boolean,
  42. isSearchServiceReachable: boolean,
  43. isSearchScopeChildrenAsDefault: boolean,
  44. drawioUri: string | null,
  45. rendererConfig: RendererConfig,
  46. };
  47. type IShareLinkRelatedPage = IPagePopulatedToShowRevision & PageDocument;
  48. superjson.registerCustom<IShareLinkRelatedPage, string>(
  49. {
  50. isApplicable: (v): v is IShareLinkRelatedPage => {
  51. return v != null
  52. && v.toObject != null
  53. && v.lastUpdateUser != null
  54. && v.creator != null
  55. && v.revision != null;
  56. },
  57. serialize: (v) => { return superjson.stringify(v.toObject()) },
  58. deserialize: (v) => { return superjson.parse(v) },
  59. },
  60. 'IShareLinkRelatedPageTransformer',
  61. );
  62. const SharedPage: NextPageWithLayout<Props> = (props: Props) => {
  63. useIsSearchPage(false);
  64. useShareLinkId(props.shareLink?._id);
  65. useCurrentPageId(props.shareLink?.relatedPage._id);
  66. useCurrentUser(props.currentUser);
  67. useCurrentPathname(props.currentPathname);
  68. useRendererConfig(props.rendererConfig);
  69. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  70. useIsSearchServiceReachable(props.isSearchServiceReachable);
  71. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  72. useDrawioUri(props.drawioUri);
  73. useIsContainerFluid(props.isContainerFluid);
  74. const growiLayoutFluidClass = useCurrentGrowiLayoutFluidClassName();
  75. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  76. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  77. const shareLink = props.shareLink;
  78. const title = generateCustomTitle(props, 'GROWI');
  79. const sideContents = shareLink != null
  80. ? <PageSideContents page={shareLink.relatedPage} />
  81. : <></>;
  82. // const footerContents = shareLink != null && isPopulated(shareLink.relatedPage.revision)
  83. // ? (
  84. // <>
  85. // <Comments pageId={shareLink._id} pagePath={shareLink.relatedPage.path} revision={shareLink.relatedPage.revision} />
  86. // </>
  87. // )
  88. // : <></>;
  89. return (
  90. <>
  91. <Head>
  92. <title>{title}</title>
  93. </Head>
  94. <div className={`dynamic-layout-root ${growiLayoutFluidClass} h-100 d-flex flex-column justify-content-between`}>
  95. <header className="py-0 position-relative">
  96. {isShowSharedPage && <GrowiContextualSubNavigation currentPage={props.shareLinkRelatedPage} isLinkSharingDisabled={props.disableLinkSharing} />}
  97. </header>
  98. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  99. <MainPane
  100. sideContents={sideContents}
  101. // footerContents={footerContents}
  102. >
  103. { props.disableLinkSharing && (
  104. <div className="mt-4">
  105. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  106. </div>
  107. )}
  108. { (isNotFound && !props.disableLinkSharing) && (
  109. <div className="container-lg">
  110. <h2 className="text-muted mt-4">
  111. <i className="icon-ban" aria-hidden="true" />
  112. <span> Page is not found</span>
  113. </h2>
  114. </div>
  115. )}
  116. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  117. <div className="container-lg">
  118. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  119. <h2 className="text-muted mt-4">
  120. <i className="icon-ban" aria-hidden="true" />
  121. <span> Page is expired</span>
  122. </h2>
  123. </div>
  124. )}
  125. {(isShowSharedPage && shareLink != null) && (
  126. <>
  127. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  128. <Page currentPage={props.shareLinkRelatedPage} />
  129. </>
  130. )}
  131. </MainPane>
  132. </div>
  133. </>
  134. );
  135. };
  136. SharedPage.getLayout = function getLayout(page) {
  137. return (
  138. <>
  139. <DrawioViewerScript />
  140. <ShareLinkLayout>{page}</ShareLinkLayout>
  141. </>
  142. );
  143. };
  144. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  145. const req: CrowiRequest = context.req as CrowiRequest;
  146. const { crowi } = req;
  147. const { configManager, searchService, xssService } = crowi;
  148. props.disableLinkSharing = configManager.getConfig('crowi', 'security:disableLinkSharing');
  149. props.isSearchServiceConfigured = searchService.isConfigured;
  150. props.isSearchServiceReachable = searchService.isReachable;
  151. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  152. props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  153. props.rendererConfig = {
  154. isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  155. isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  156. adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  157. isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  158. plantumlUri: process.env.PLANTUML_URI ?? null,
  159. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  160. // XSS Options
  161. isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:rehypeSanitize:isEnabledPrevention'),
  162. attrWhiteList: xssService.getAttrWhiteList(),
  163. tagWhiteList: xssService.getTagWhiteList(),
  164. highlightJsStyleBorder: configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  165. };
  166. }
  167. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  168. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  169. props._nextI18Next = nextI18NextConfig._nextI18Next;
  170. }
  171. function getAction(props: Props): SupportedActionType {
  172. let action: SupportedActionType;
  173. if (props.isExpired) {
  174. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  175. }
  176. else if (props.shareLink == null) {
  177. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  178. }
  179. else {
  180. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  181. }
  182. return action;
  183. }
  184. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  185. const req: CrowiRequest = context.req as CrowiRequest;
  186. const parameters = {
  187. ip: req.ip,
  188. endpoint: req.originalUrl,
  189. action,
  190. user: req.user?._id,
  191. snapshot: {
  192. username: req.user?.username,
  193. },
  194. };
  195. await req.crowi.activityService.createActivity(parameters);
  196. }
  197. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  198. const req = context.req as CrowiRequest<IUserHasId & any>;
  199. const { crowi, params } = req;
  200. const result = await getServerSideCommonProps(context);
  201. if (!('props' in result)) {
  202. throw new Error('invalid getSSP result');
  203. }
  204. const props: Props = result.props as Props;
  205. try {
  206. const ShareLinkModel = crowi.model('ShareLink');
  207. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  208. if (shareLink != null) {
  209. props.shareLinkRelatedPage = await shareLink.relatedPage.populateDataToShowRevision();
  210. props.isExpired = shareLink.isExpired();
  211. props.shareLink = shareLink.toObject();
  212. }
  213. }
  214. catch (err) {
  215. logger.error(err);
  216. }
  217. injectServerConfigurations(context, props);
  218. await injectNextI18NextConfigurations(context, props);
  219. await addActivity(context, getAction(props));
  220. return {
  221. props,
  222. };
  223. };
  224. export default SharedPage;